import Options

def configure(conf) :
    if Options.options.matlab :
        #check if mex is in path .. 
        conf.env.matlab = True
        conf.find_program("mex",mandatory=True)
        conf.env['MEX_COMP'] = "mex"
        conf.env['MEX_EXT'] = "mexglx"

    if Options.options.octave :
        conf.env.octave = True
        conf.find_program("mkoctfile")
        conf.env['MEX_COMP'] = "mkoctfile --mex"
        conf.env['MEX_EXT'] = "mex"

def build(bld) :
    """
    this is really ugly, but sort of work .. 
    we should probably define a 'cxx cshlib features' 
    (see python Tool code ) 
    that override compiler/linker to use mex_comp, and 
    uselib and everything .. 
    """
    obj_src = bld.path.ant_glob("*.c")
    fgmm_task_gen = bld.name_to_obj('fgmm',bld.env)
    fgmm_task_gen.post()
    inc_ = fgmm_task_gen.path.srcpath(bld.env)
    libp_ = fgmm_task_gen.path.abspath(bld.env)

    for mx_file in obj_src.split() :
        m = bld( rule =  "${MEX_COMP} ${SRC} -I%s -L%s -lfgmm -o ${TGT}"%(inc_,libp_),
                 source = [mx_file],
                 target = mx_file.split('.')[0]+"."+bld.env["MEX_EXT"],
                 after = fgmm_task_gen.link_task.name)
        
                 
      
           

# import TaskGen

# import Task
# Task.simple_task_type('mex',
#                       'echo ${SRC} ${TGT}', 
#                       color='PINK')

# @TaskGen.feature("mex")
# def compile(self) :
#     self.env['MEXCOMP'] = 'mex'
#     for ulib in self.to_list(self.uselib_local):
#         other = self.bld.name_to_obj(ulib,self.env)
#         other.post()
#     self.create_task("mex",self.source)
